package com.example.androworld;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.widget.*;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.*;
import android.view.View.OnClickListener;

public class AndoWorld extends Activity {
    /** Called when the activity is first created. */
	ArrayList<PowerCard> powercards;
	int i = 0;
	int[] pcPngIds = new int[10];
    String[] pcPngNames = {"dnd_power_black", "dnd_power_blue", "dnd_power_cyan"}; 
    Dialog dialog;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout ll = (LinearLayout) findViewById(R.id.lay);
        ll.setOnClickListener(llListener);
        TextView ft = (TextView) findViewById(R.id.flavor_text);
        ft.setOnClickListener(ftListener);
        i = 0;
        for (String pcPngName : pcPngNames ) {
            pcPngIds[i] = getResources().getIdentifier(pcPngName, 
            		   								   "drawable", 
            										   "com.example.androworld");
            i++;
        }
        try{
            int id = getResources().getIdentifier("power_cards", "xml", "com.example.androworld");
            XmlResourceParser xml = getResources().getXml(id);
            powercards = PowerCard.xml_to_pc(xml);
        }catch (Exception e){
        	//((TextView) findViewById(R.id.tx)).setText("mikou");
        }
        dialog = new Dialog(AndoWorld.this);
        dialog.setContentView(R.layout.text_dialog);
        dialog.setTitle("Custom Dialog");
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Hello, this is a custom dialog!");
    }
     
	private void nextPowerCard(){
    	if (i>powercards.size()-2) i = 0; else i++;
    	PowerCard cur_pc = powercards.get(i);
        ((TextView) findViewById(R.id.title)).setText(cur_pc.title);
        ((TextView) findViewById(R.id.flavor_text)).setText(cur_pc.flavor_text);
        ((TextView) findViewById(R.id.freq_kw)).setText(cur_pc.frequency + " • " + 
        		                                        TextUtils.join(", ", cur_pc.keywords));
        ((TextView) findViewById(R.id.action_type)).setText(cur_pc.action_type);
        Spannable atr = new SpannableString(cur_pc.attack_type + " " + cur_pc.range);
        atr.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, cur_pc.attack_type.length(),
        		                                               Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ((TextView) findViewById(R.id.attack_type_range)).setText(atr);
        ((LinearLayout) findViewById(R.id.lay)).setBackgroundResource(pcPngIds[i]);

	}
	
    private OnClickListener llListener = new OnClickListener()
    {
        public void onClick(View v)
        {
        	nextPowerCard();
        }
    };
    
    private OnClickListener ftListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            dialog.setTitle("Flavor Text");
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText(powercards.get(i).flavor_text);
	        dialog.show();
        }
    };
}